package com.wf.demo.info.service;

import com.wf.demo.info.dao.MatchDAO;
import com.wf.demo.info.dao.ParseDAO;
import com.wf.demo.info.pojo.Match;
import com.wf.demo.info.pojo.Model;
import com.wf.demo.info.pojo.Parse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class MatchService {
    @Autowired
    MatchDAO matchDAO;
    @Autowired
    ParseDAO parseDAO;
    public List<Match> list(int pid){
        Parse parse=parseDAO.findOne(pid);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        List<Match> list= matchDAO.findByParse(parse ,sort);
        return list;
    }
    public void delete(int id){
        matchDAO.delete(id);
    }
    public void update(Match bean){
        matchDAO.save(bean);
    }
    public void add(Match bean){
        matchDAO.save(bean);
    }
    public Match get(int id){
        return matchDAO.findOne(id);
    }
    public boolean isExist(BigDecimal dataValue,Parse parse) {

        Match match=matchDAO.findByParseAndDataValue(parse,dataValue);
        return null!=match;
    }
    public boolean isExist(Match bean) {
        int id =bean.getId();
        Match match=matchDAO.getOne(id);

        BigDecimal dataValue=bean.getDataValue();
        Match match1=matchDAO.findByParseAndDataValue(bean.getParse(),dataValue);
        // 数值确实重复了
        if(null!=match1 && !match.getDataValue().equals(bean.getDataValue()))
            return true;
        else{
            return false;
        }
    }
    /**
     *功能描述  对于需要匹配的文字信息的数据输出 数值+文字信息
     * @author wf
     * @date 2019/11/26
     * @param parse
     * @param outValue
     * @return java.lang.String
     */
    public String getStringByMatch(Parse parse,BigDecimal outValue){
        boolean isMatch =parse.isMatch();
        String outString =new String();//输出值+文字信息
        if(isMatch == true){//匹配文字信息0：不需要， 1：需要
            Match match=matchDAO.findByParseAndDataValue(parse,outValue);
            //空指针异常判断
           if(null != match){
               String infoMatch=match.getInfoMatch();
               outString =outString.concat(infoMatch);
               System.out.println("解析的最后信息是"+outString);
           }
           else {
               System.out.println("状态正常，不需要匹配解析信息");
               outString="默认正常";
           }
        }
        return outString;
    }
    /**
     *功能描述   对运行状态status文字信息初始化
     * @author wf
     * @date 2019/12/9
     * @param model
     * @return void
     */
    public void addStatusParseInit(Model model){
        //得到运行状态status的parse
        Parse parse=parseDAO.findByModelAndAndDataIdentifier(model,"status");
        List<Match> statusMatches=new ArrayList<>();
        Match statusMatch1=new Match(parse,"运行状态",new BigDecimal(0),"未同步");
        Match statusMatch2=new Match(parse,"运行状态",new BigDecimal(1),"已同步");
        Match statusMatch3=new Match(parse,"运行状态",new BigDecimal(2),"正常起动");
        Match statusMatch4=new Match(parse,"运行状态",new BigDecimal(3),"凸轮起动");
        Match statusMatch5=new Match(parse,"运行状态",new BigDecimal(4),"起动失败");
        Match statusMatch6=new Match(parse,"运行状态",new BigDecimal(5),"正常运行");
        Match statusMatch7=new Match(parse,"运行状态",new BigDecimal(6),"人工停机");
        Match statusMatch8=new Match(parse,"运行状态",new BigDecimal(7),"紧急停机");
        Match statusMatch9=new Match(parse,"运行状态",new BigDecimal(8),"功率限制");
        Match statusMatch10=new Match(parse,"运行状态",new BigDecimal(9),"凸轮跛行");
        statusMatches.add(statusMatch1);
        statusMatches.add(statusMatch2);
        statusMatches.add(statusMatch3);
        statusMatches.add(statusMatch4);
        statusMatches.add(statusMatch5);
        statusMatches.add(statusMatch6);
        statusMatches.add(statusMatch7);
        statusMatches.add(statusMatch8);
        statusMatches.add(statusMatch9);
        statusMatches.add(statusMatch10);
        for (Match match: statusMatches){
            matchDAO.save(match);
        }
    }
    /**
     *功能描述  对故障码初始化
     * @author wf
     * @date 2019/12/9
     * @param model
     * @return void
     */
    public void addAlarmCodeParseInit(Model model){
        //得到运行状态status的parse
        Parse parse=parseDAO.findByModelAndAndDataIdentifier(model,"alarmCode");
        List<Match> alarmCodeMatches=new ArrayList<>();
        Match alarmCodeMatch1=new Match(parse,"故障码",new BigDecimal(1),"本地紧急停机-测试");
        Match alarmCodeMatch2=new Match(parse,"故障码",new BigDecimal(2),"本地紧急停机-误触碰");
        Match alarmCodeMatch3=new Match(parse,"故障码",new BigDecimal(3),"本地紧急停机-电控系统综合故障");
        Match alarmCodeMatch4=new Match(parse,"故障码",new BigDecimal(4),"本地紧急停机-其他");
        Match alarmCodeMatch5=new Match(parse,"故障码",new BigDecimal(9),"油泵齿条位置传感器故障");
        Match alarmCodeMatch6=new Match(parse,"故障码",new BigDecimal(32769),"油泵齿条位置值小于下限值");
        Match alarmCodeMatch7=new Match(parse,"故障码",new BigDecimal(32772),"瞬时转速大于报警上限值");
        alarmCodeMatches.add(alarmCodeMatch1);
        alarmCodeMatches.add(alarmCodeMatch2);
        alarmCodeMatches.add(alarmCodeMatch3);
        alarmCodeMatches.add(alarmCodeMatch4);
        alarmCodeMatches.add(alarmCodeMatch5);
        alarmCodeMatches.add(alarmCodeMatch6);
        alarmCodeMatches.add(alarmCodeMatch7);

        for (Match match: alarmCodeMatches){
            matchDAO.save(match);
        }
    }

}
